import Vue, { createApp, defineAsyncComponent } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//import '@/custom-component'   这个文件的功能改在main.js中

import '@/styles/reset.css'
import '@/styles/global.scss'

import '@/assets/iconfont/iconfont.css'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

import zhCn from 'element-plus/es/locale/lang/zh-cn'

const  app = createApp(App)

app.use(store).use(router).use(ElementPlus,{ locale: zhCn, size: 'small'}).mount('#app')

// import VText from '@/custom-component/VText/Component'
// import VTextAttr from '@/custom-component/VText/Attr'
//
// app.component('VText', VText )
// app.component('VTextAttr', VTextAttr )

function registerComponents(requireContext, vuename ) {
    requireContext.keys().forEach(fileName => {
        // 获取组件配置
        const componentConfig = requireContext(fileName);
        // console.log( 'filename:', fileName )
        // 获取文件名并移除路径和后缀，只保留组件名
        const componentName = fileName
            .split('/')
            .pop()
            .replace(/\.\w+$/, '');
        // console.log( "filename: ",  componentName )

        // 全局注册组件
        if ( componentName === 'Component' ){
            app.component(
                vuename,
                defineAsyncComponent(() => Promise.resolve(componentConfig.default || componentConfig))
            );
        }
        // 全局注册组件
        if ( componentName === 'Attr' ){
            app.component(
                vuename + 'Attr' ,
                defineAsyncComponent(() => Promise.resolve(componentConfig.default || componentConfig))
            );
        }
    });
}

const componentsPath = [
    'VText'
    // 'CircleShape',
    // 'Picture',
    // 'VButton',
    // 'Group',
    // 'RectShape',
    // 'LineShape',
    // 'VTable',
    // 'VChart',
]

//注册来自 `custom-component/目录下的组件` 目录的组件
const componentsDir1 = require.context('@/custom-component/VText', false, /\.vue$/);
registerComponents(componentsDir1, 'VText' );
const componentsDir2 = require.context('@/custom-component/CircleShape', false, /\.vue$/);
registerComponents(componentsDir2, 'CircleShape' );
const componentsDir3 = require.context('@/custom-component/LineShape', false, /\.vue$/);
registerComponents(componentsDir3, 'LineShape' );
const componentsDir4 = require.context('@/custom-component/RectShape', false, /\.vue$/);
registerComponents(componentsDir4, 'RectShape' );
const componentsDir5 = require.context('@/custom-component/VButton', false, /\.vue$/);
registerComponents(componentsDir5, 'VButton' );
const componentsDir6 = require.context('@/custom-component/Picture', false, /\.vue$/);
registerComponents(componentsDir6, 'Picture' );
const componentsDir7 = require.context('@/custom-component/WinSvg', false, /\.vue$/);
registerComponents(componentsDir7, 'WinSvg' );
const componentsDir8 = require.context('@/custom-component/Group', false, /\.vue$/);
registerComponents(componentsDir8, 'Group' );
const componentsDir9 = require.context('@/custom-component/VChart', false, /\.vue$/);
registerComponents(componentsDir9, 'VChart' );
const componentsDir10 = require.context('@/custom-component/VMeter', false, /\.vue$/);
registerComponents(componentsDir10, 'VMeter' );
const componentsDir11 = require.context('@/custom-component/VTable', false, /\.vue$/);
registerComponents(componentsDir11, 'VTable' );
const componentsDir12 = require.context('@/custom-component/VRealCurve', false, /\.vue$/);
registerComponents(componentsDir12, 'VRealCurve' );


// const svgs = [
//     'SVGStar',
//     'SVGTriangle',
// ]

// svgs.forEach(key => {
//     const svg = require.context('@/custom-component/svgs/${key}', false, /\.vue$/);
//     registerComponents(svg, key );
// })

//消除el-table引起的错误提醒
const debounce = (fn, delay) => {
    let timer
     return (...args) => {
       if (timer) {
         clearTimeout(timer)
       }
       timer = setTimeout(() => {
         fn(...args)
       }, delay)
     }
  }
    
  const _ResizeObserver = window.ResizeObserver;
  window.ResizeObserver = class ResizeObserver extends _ResizeObserver{
     constructor(callback) {
       callback = debounce(callback, 200);
       super(callback);
     }
  }